package com.yyt.module_main.act;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.yyt.libsrc.bean.BasicDocumentBean;
import com.yyt.libsrc.bean.ConfigBean;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.net.JavaParamsBuilder;
import com.yyt.libsrc.net.NetUtil;
import com.yyt.libsrc.net.SuccessCallback;
import com.yyt.libsrc.router.RouteTable;
import com.yyt.module_main.R;
import com.yyt.module_main.databinding.ActivityUserInfoBinding;
import com.yyt.module_main.vm.UserInfoVM;

import top.lixb.libcommon.base.RefreshableActivity;
import top.lixb.libcommon.helper.GlideHelper;
import top.lixb.libcommon.utils.StringUtils;

@Route(path = RouteTable.main_user_info)
public class UserInfoActivity extends RefreshableActivity<ActivityUserInfoBinding> {

    private UserInfoVM mVm;

    @Override
    protected void init() {
        mVm = new UserInfoVM();
        mRefreshContentBinding.setVm(mVm);
        setTitle("会员等级");
        showToolbarDivider();
    }

    @Override
    protected int initRefreshLayout() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void getData(boolean refresh) {
        showLoading();
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.basicDocument)
                .build(), new SuccessCallback<BasicDocumentBean>() {
            @Override
            public void onSuccess(BasicDocumentBean basicDocumentBean) {
                if (null != mVm) {
                    GlideHelper.loadImg(mRefreshContentBinding.ivIcon,basicDocumentBean.data.headImg);
                    mVm.username.set(basicDocumentBean.data.realName);
                    mVm.rate.set(StringUtils.doubleFormat(String.valueOf(basicDocumentBean.data.rate))+"%");
                    mVm.DMode.set("D0");
                    mVm.memberCount.set("不限");
                    if (basicDocumentBean.data.role == 1) {
                        mVm.level.set("成员");
                        mRefreshContentBinding.rlForeverFr.setVisibility(View.GONE);
                        mRefreshContentBinding.rlAgentCount.setVisibility(View.GONE);
                    } else if (basicDocumentBean.data.role == 2) {
                        mVm.level.set("代理商");
                        mRefreshContentBinding.rlForeverFr.setVisibility(View.GONE);
                        mRefreshContentBinding.rlAgentCount.setVisibility(View.GONE);
                    } else if (basicDocumentBean.data.role == 3) {
                        mVm.level.set("合伙人");
                        mRefreshContentBinding.rlForeverFr.setVisibility(View.VISIBLE);
                        mRefreshContentBinding.rlAgentCount.setVisibility(View.VISIBLE);
                        getSetting();
                    }
                }


            }

            @Override
            public void onComplete() {
                super.onComplete();
                finishGetData();
                hideLoading();
            }
        });
    }

    private void getSetting() {
        //获取永久收益
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.configuration)
                .addP("name","permanent_income")
                .build(), new SuccessCallback<ConfigBean>() {
            @Override
            public void onSuccess(ConfigBean configBean) {
                int fee = (int)(Double.valueOf(configBean.data.value)*10000);
                mVm.rateforever.set("万分之"+toChinese(String.valueOf(fee)));
            }
        });
    }


    private static String toChinese(String str) {
        String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };
        String result = "";
        int n = str.length();
        for (int i = 0; i < n; i++) {
            int num = str.charAt(i)-'0';
            if (i != n - 1 && num != 0) {
                result += s1[num] + s2[n - 2 - i];
            } else {
                result += s1[num];
            }
        }
        return result;
    }
}
